<?php

/**
 * This is the model class for table "property".
 *
 * The followings are the available columns in table 'property':
 * @property integer $id
 * @property string $unit_number
 * @property string $unit_type
 * @property string $unit_area
 * @property double $contract_price
 * @property integer $Project_id
 *
 * The followings are the available model relations:
 * @property Project $project
 */
class Property extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Property the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'property';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('Project_id', 'required'),
			array('Project_id, views, views2', 'numerical', 'integerOnly'=>true),
			array('contract_price', 'numerical'),
			array('unit_number, unit_type, unit_area', 'length', 'max'=>45),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, unit_number, unit_type, unit_area, contract_price, Project_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'project' => array(self::BELONGS_TO, 'Project', 'Project_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Property ID',
			'unit_number' => 'Unit Number',
			'unit_type' => 'Unit Type',
			'unit_area' => 'Unit Area',
			'contract_price' => 'Contract Price',
			'Project_id' => 'Project ID',
			'views' => 'Views',
			'views2' => 'No. of Price Quote Requests'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('unit_number',$this->unit_number,true);
		$criteria->compare('unit_type',$this->unit_type,true);
		$criteria->compare('unit_area',$this->unit_area,true);
		$criteria->compare('contract_price',$this->contract_price);
		$criteria->compare('Project_id',$this->Project_id);
		$criteria->compare('views',$this->views);
		$criteria->compare('views2',$this->views2);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getPropertyByProjectID($id)
	{
		return $this->model()->findAll('`Project_id` = '.$id);
	}
	
	public function scopes()
    {
        return array(
            'mostViewed'=>array(
                'order'=>'`views` DESC',
                'limit'=>3,
            ),
			'mostRequested'=>array(
                'order'=>'`views2` DESC',
                'limit'=>3,
            ),
        );
    }
}